package ru.spies.entities;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;


/**
 * The persistent class for the roles database table.
 * 
 */
@Entity
@Table(name="roles")
@NamedQuery(name="Role.findAll", query="SELECT r FROM Role r")
public class Role implements Serializable, ITableClass {
	private static final long serialVersionUID = 1L;
		
	private Object paramForFindQuery=null;

	@Id
	@GeneratedValue
	@Column(unique=true, nullable=false)
	private Integer id;

	@Column(nullable=false, length=2147483647)
	private String name;

	//bi-directional many-to-one association to Siteuser
	@OneToMany(mappedBy="role", fetch = EAGER, cascade = ALL)
	private List<Siteuser> siteusers;
	
	public Role() {
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Siteuser> getSiteusers() {
		return this.siteusers;
	}

	public void setSiteusers(List<Siteuser> siteusers) {
		this.siteusers = siteusers;
	}

	public Siteuser addSiteuser(Siteuser siteuser) {
		getSiteusers().add(siteuser);
		siteuser.setRole(this);

		return siteuser;
	}

	public Siteuser removeSiteuser(Siteuser siteuser) {
		getSiteusers().remove(siteuser);
		siteuser.setRole(null);

		return siteuser;
	}

	@Override
	public void setParamForFindQuery(Object param) {
		paramForFindQuery=(String) param;
	}

	@Override
	public Object getParamForFindQuery() {
		return paramForFindQuery;
	}

	@Override
	public ITableClass createThisObject() {
		return new Role();
	}

	@Override
	public String getQueryForFind() {
		return "SELECT r FROM Role r WHERE r.name = :param";
	}

}